﻿(function($){
     var Lottery = function (options) {

        this.option = options;

        this.len = $(this.option.dom).length;
        this.init()
    };
    
    Lottery.prototype.init = function () {
        var _op = this.option,
            _len = this.len,
            _i = this.getSite($(_op.dom), _op.id);

        this.SyncView(0);

        this.initValue = 0;
        this.time = 0;
        this.circleFnc((_len * _op.circle) + (_i + 1))

    };

    //工具 得出当前位置
    Lottery.prototype.getSite = function (_d, _n) {
        var nI;
        _d.each(function (a) {
            if ($(this).attr("data-id") == _n) {

                nI = a;
                return false
            }
        });
        return nI
    };

    Lottery.prototype.circleFnc = function (num) {
        var _this = this, _op = this.option, _len = this.len;
        _op.time += 5;
        if (num <= 0) {
            _op.endCall()
//          console.log("结束")
            return false
        } else {
            setTimeout(function () {
                if (_this.initValue >= _len) {
                    _this.initValue = 0;
                }
                _this.SyncView(_this.initValue++);
                _this.circleFnc(--num)
            }, _op.time)

        }
    };

    Lottery.prototype.SyncView = function (i) {
        var _op = this.option;
        $(_op.dom).eq(i).addClass(_op.active).siblings(_op.dom).removeClass(_op.active);
    };
    window.lottery = Lottery;
    
    
})(Zepto)